<?php
namespace app\socket\controller;

use think\Controller;
use app\common\controller\Curls;

class Index extends Controller
{
	/**
	 * IP
	 * @var string
	 */
	private $host = "0.0.0.0";
	
	/**
	 * 端口号
	 * @var int
	 */
	private $post = "1235";
	
	/**
	 * websocket对象
	 * @var Object
	 */
	private $serve;
	
	
	public function index()
	{
		$this->serve = new \swoole_websocket_server($this->host,$this->post);
		$this->serve->set([
				'worker_num' =>  1,  //进程数
				'daemonize' => true,  //开启守护进程模式
				'task_worker_num' => 200
		]);
		$this->serve->on('start',[$this,'onStart']);
		$this->serve->on('open',[$this,'onOpen']);
		$this->serve->on('task',[$this,'onTask']);
		$this->serve->on('finish',[$this,'onFinish']);
		$this->serve->on('message',[$this,'onMessage']);
		$this->serve->on('close',[$this,'onClose']);
		$this->serve->start();
	}
	
	public function onStart()
	{
		swoole_timer_tick(1000, function ($timer_id) {
			Curls::curl('http://coin.wlwrx.cn/sched/Trade/tradeLeveleraged');
		});
	}
	
	public function onFinish($server,$request)
	{
	}
	public function onTask($server,$request,$data)
	{
	}
	/**
	 * 客户端连接成功时间
	 * @param object $server
	 * @param object $request
	 */
	public function onOpen($server,$request)
	{
	}
	
	/**
	 * 客户端发送消息事件
	 * @param object $server
	 * @param object $request
	 */
	public function onMessage($server,$request)
	{
	}
	
	/**
	 * 客户端关闭事件
	 * @param object $server
	 * @param int $request  客户端唯一标识fd
	 */
	public function onClose($server,$request)
	{
	}
}